package com.ruoyi.system.frontierCommand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.BaseException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionAddBo;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionQueryBo;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionEditBo;
import com.ruoyi.system.frontierCommand.domain.TPatrolMission;
import com.ruoyi.system.frontierCommand.mapper.TPatrolMissionMapper;
import com.ruoyi.system.frontierCommand.vo.TPatrolMissionVo;
import com.ruoyi.system.frontierCommand.service.ITPatrolMissionService;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 巡护任务Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-19
 */
@Service
public class TPatrolMissionServiceImpl extends ServiceImpl<TPatrolMissionMapper, TPatrolMission> implements ITPatrolMissionService {

    @Override
    public TPatrolMissionVo queryById(Long id) {
//        TPatrolMission db = this.baseMapper.selectById(id);
        TPatrolMissionVo db = this.baseMapper.selectMissionById(id);
        return BeanUtil.toBean(db, TPatrolMissionVo.class);
    }

    @Override
    public List<TPatrolMissionVo> queryList(TPatrolMissionQueryBo bo) {
//        LambdaQueryWrapper<TPatrolMission> lqw = Wrappers.lambdaQuery();
//        lqw.like(StrUtil.isNotBlank(bo.getMission()), TPatrolMission::getMission, bo.getMission());
//        lqw.eq(bo.getTaskBeginTime() != null, TPatrolMission::getTaskBeginTime, bo.getTaskBeginTime());
//        lqw.eq(bo.getTaskEndTime() != null, TPatrolMission::getTaskEndTime, bo.getTaskEndTime());
//        return entity2Vo(this.list(lqw));
        return this.baseMapper.selectDTO(bo);
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TPatrolMissionVo> entity2Vo(Collection<TPatrolMission> collection) {
        List<TPatrolMissionVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TPatrolMissionVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TPatrolMission> page = (Page<TPatrolMission>) collection;
            Page<TPatrolMissionVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TPatrolMissionAddBo bo) {
        TPatrolMission add = BeanUtil.toBean(bo, TPatrolMission.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TPatrolMissionEditBo bo) {
        TPatrolMission update = BeanUtil.toBean(bo, TPatrolMission.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TPatrolMission entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
//        if (isValid) {
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        TPatrolMission tPatrolMission = new TPatrolMission();
//        Date currentTime = new Date();
//        for (Long id : ids) {
//            tPatrolMission = this.getById(id);
//            if (tPatrolMission.getTaskBeginTime().compareTo(currentTime) < 0 && tPatrolMission.getTaskEndTime().compareTo(currentTime) > 0) {
//
//            }else {
//                this.removeById(id);
//            }
//        }
//        return true;
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
            for (Long id : ids) {
                TPatrolMission tPatrolMission = this.baseMapper.selectById(id);
                if(Validator.isNotEmpty(tPatrolMission.getTaskBeginTime())&&Validator.isEmpty(tPatrolMission.getTaskEndTime())){
                    throw  new BaseException("所选择的巡护任务有未结束的任务请重新选择！");
                }else{
                    return this.removeById(id);
                }
            }
        }
        return this.removeByIds(ids);
    }

    @Override
    public String checkNameUnique(String name, int id) {
        LambdaQueryWrapper<TPatrolMission> lqw = Wrappers.lambdaQuery();
        if (id != 0)
            lqw.notIn(TPatrolMission::getId, id);
        lqw.eq(TPatrolMission::getMission, name);
        lqw.eq(TPatrolMission::getDelFlag, "0");
        lqw.last("limit 1");
        int count = count(lqw);
        if (count > 0) {
            return "1";
        }
        return "0";
    }
}
